package com.lishe.lishe.lishe.module.mall.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.mall.adapter.GoodsAddressAdapter;
import com.lishe.lishe.lishe.bean.AddressItem;

import java.util.List;

/**
 * 地址选择弹窗
 * @author Bin
 */
public class GoodsAddressDialog extends Dialog implements View.OnClickListener {

	private Context context;
	private SelectListener mListener;
	private List<AddressItem> mAddressList;
	private GoodsAddressAdapter mAdapter;
	private RecyclerView mRecyclerView;

	public interface SelectListener {
		void onSelect(int position);
		void onNewAddress();
	}

	public void resetData(List<AddressItem> list) {
		mAddressList = list;
		mAdapter = new GoodsAddressAdapter(context, mAddressList);
		mAdapter.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
			@Override
			public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
				for (int i = 0; i < mAddressList.size(); i++) {
					mAddressList.get(i).setCheck(false);
				}
				mAddressList.get(position).setCheck(true);
				mListener.onSelect(position);
				dismiss();
			}
		});
		mRecyclerView.setAdapter(mAdapter);
	}

	public GoodsAddressDialog(Context context, List<AddressItem> list, SelectListener listener) {
		super(context, R.style.SimpleBottomDialog);
		this.context = context;
		this.mAddressList = list;
		this.mListener = listener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_goodsaddress);
		Window dialogWindow = this.getWindow();
		dialogWindow.setGravity(Gravity.BOTTOM);

		Display display = dialogWindow.getWindowManager().getDefaultDisplay();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.width = display.getWidth();
		dialogWindow.setAttributes(lp);

		//设置点击Dialog外部任意区域关闭Dialog
		setCanceledOnTouchOutside(true);

		mRecyclerView = findViewById(R.id.recyclerview);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
		mAdapter = new GoodsAddressAdapter(context, mAddressList);
		mAdapter.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
			@Override
			public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
				for (int i = 0; i < mAddressList.size(); i++) {
					mAddressList.get(i).setCheck(false);
				}
				mAddressList.get(position).setCheck(true);
				mListener.onSelect(position);
				dismiss();
			}
		});
		mRecyclerView.setAdapter(mAdapter);

		findViewById(R.id.cancel).setOnClickListener(this);
		findViewById(R.id.new_address).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.cancel:
				dismiss();
				break;
			case R.id.new_address:
				mListener.onNewAddress();
				break;
		}
	}

}
